{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "### 列表运算"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[] <class 'list'>\n",
      "[1, 2, 3, 4, 5] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# 空列表初始化\n",
    "lst = []\n",
    "print(lst, type(lst))\n",
    "\n",
    "# 列表初始化\n",
    "lst = [1, 2, 3, 4, 5]\n",
    "print(lst, type(lst))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a', 'b', 1, 2, 3] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "lst = ['a', 'b', 1, 2, 3]\n",
    "print(lst, type(lst))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# 使用加号（+）对列表进行运算\n",
    "l1 = [1, 2]\n",
    "l2 = [3, 4]\n",
    "lst = l1 + l2\n",
    "print(lst, type(lst))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 'X', 3, 4] <class 'list'>\n",
      "[1, 'Z', 2, 'X', 3, 4] <class 'list'>\n",
      "[1, 'Z', 2, 'X', 3, 'T', 4] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# 使用insert方法向列表中添加元素\n",
    "lst = [1, 2, 3, 4]\n",
    "\n",
    "lst.insert(2, 'X')\n",
    "print(lst, type(lst))\n",
    "\n",
    "lst.insert(1, 'Z')\n",
    "print(lst, type(lst))\n",
    "\n",
    "lst.insert(5, 'T')\n",
    "print(lst, type(lst))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5] <class 'list'>\n",
      "[1, 2, 3, 4, 5, 6] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# 使用append向列表添加元素\n",
    "lst = [1, 2, 3, 4]\n",
    "\n",
    "lst.append(5)\n",
    "print(lst, type(lst))\n",
    "\n",
    "lst.append(6)\n",
    "print(lst, type(lst))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, [3, 4]] <class 'list'>\n",
      "[1, 2, 3, 4, [3, 4], [5, 6]] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# 使用append向列表中添加列表\n",
    "lst = [1, 2, 3, 4]\n",
    "\n",
    "l1 = [3, 4]\n",
    "lst.append(l1)\n",
    "print(lst, type(lst))\n",
    "\n",
    "l2 = [5, 6]\n",
    "lst.append(l2)\n",
    "print(lst, type(lst))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 'a', 'b'] <class 'list'>\n",
      "[1, 2, 3, 4, 'a', 'b', 'c', 'd'] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# 使用extend向列表添加元素\n",
    "lst = [1, 2, 3, 4]\n",
    "\n",
    "l1 = ['a', 'b']\n",
    "lst.extend(l1)\n",
    "print(lst, type(lst))\n",
    "\n",
    "l2 = ['c', 'd']\n",
    "lst.extend(l2)\n",
    "print(lst, type(lst))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 'a', 'b', 'c'] <class 'list'>\n",
      "[1, 2, 3, 4, 'a', 'b'] 返回结果： c\n",
      "[1, 2, 4, 'a', 'b'] 返回结果： 3\n"
     ]
    }
   ],
   "source": [
    "# 使用pop方法从列表中弹出元素\n",
    "lst = [1, 2, 3, 4, 'a', 'b', 'c']\n",
    "\n",
    "print(lst, type(lst))\n",
    "\n",
    "rst = lst.pop()\n",
    "print(lst, \"返回结果：\", rst)\n",
    "\n",
    "# 弹出指定位置的元素\n",
    "rst = lst.pop(2)\n",
    "print(lst, \"返回结果：\", rst)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 'a', 'b', 'c', 'a', 'b', 4] <class 'list'>\n",
      "[1, 2, 3, 4, 'b', 'c', 'a', 'b', 4] <class 'list'>\n",
      "[1, 2, 3, 'b', 'c', 'a', 'b', 4] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# 使用remove删除列表中的某个元素\n",
    "# 列表中第一个匹配的元素将被删除\n",
    "lst = [1, 2, 3, 4, 'a', 'b', 'c', 'a', 'b', 4]\n",
    "\n",
    "print(lst, type(lst))\n",
    "\n",
    "lst.remove('a')\n",
    "print(lst, type(lst))\n",
    "\n",
    "lst.remove(4)\n",
    "print(lst, type(lst))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 'a', 'b', 'c'] <class 'list'>\n",
      "3\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "# 使用下标访问列表元素\n",
    "lst = [1, 2, 3, 4, 'a', 'b', 'c']\n",
    "\n",
    "print(lst, type(lst))\n",
    "\n",
    "print(lst[2])\n",
    "print(lst[4])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 'a', 'b', 'c'] <class 'list'>\n",
      "[1, 2, 'Hello', 4, 'a', 'ZT', 'c'] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# 使用下标修改列表元素\n",
    "lst = [1, 2, 3, 4, 'a', 'b', 'c']\n",
    "\n",
    "print(lst, type(lst))\n",
    "\n",
    "lst[2] = 'Hello'\n",
    "lst[5] = 'ZT'\n",
    "\n",
    "print(lst, type(lst))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 'a', 'b', 'c'] <class 'list'>\n",
      "[1, 2, 3, 'a', 'b', 'c'] <class 'list'>\n",
      "[1, 2, 3, 'a', 'c'] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# 使用del删除列表中的某个元素\n",
    "lst = [1, 2, 3, 4, 'a', 'b', 'c']\n",
    "\n",
    "print(lst, type(lst))\n",
    "\n",
    "del lst[3]\n",
    "print(lst, type(lst))\n",
    "\n",
    "del lst[4]\n",
    "print(lst, type(lst))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 'a', 'b', 'c'] <class 'list'>\n",
      "the index position of value 'a' in the lst is 4.\n",
      "the index position of value 3 in the lst is 2.\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "'a' is not in list",
     "output_type": "error",
     "traceback": [
      "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[0;31mValueError\u001B[0m                                Traceback (most recent call last)",
      "\u001B[0;32m/var/folders/mr/zthpdwtn37g2h4t97k6tv1pw0000gn/T/ipykernel_5080/646454911.py\u001B[0m in \u001B[0;36m<module>\u001B[0;34m\u001B[0m\n\u001B[1;32m      6\u001B[0m \u001B[0mprint\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0;34mf\"the index position of value 'a' in the lst is {lst.index('a')}.\"\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m      7\u001B[0m \u001B[0mprint\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0;34mf\"the index position of value 3 in the lst is {lst.index(3)}.\"\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0;32m----> 8\u001B[0;31m \u001B[0mprint\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0;34mf\"the index position of value 'a' in the lst is {lst.index('a', 0, 3)}.\"\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0m",
      "\u001B[0;31mValueError\u001B[0m: 'a' is not in list"
     ]
    }
   ],
   "source": [
    "# 使用index返回某个元素的索引位置\n",
    "lst = [1, 2, 3, 4, 'a', 'b', 'c']\n",
    "\n",
    "print(lst, type(lst))\n",
    "\n",
    "print(f\"the index position of value 'a' in the lst is {lst.index('a')}.\")\n",
    "print(f\"the index position of value 3 in the lst is {lst.index(3)}.\")\n",
    "\n",
    "# 指定索引位置的范围进行查找\n",
    "print(f\"the index position of value 'a' in the lst is {lst.index('a', 0, 3)}.\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 'a', 'b', 'c'] <class 'list'>\n",
      "列表中包含7个元素\n"
     ]
    }
   ],
   "source": [
    "# 获取列表中的元素个数\n",
    "lst = [1, 2, 3, 4, 'a', 'b', 'c']\n",
    "\n",
    "print(lst, type(lst))\n",
    "\n",
    "print(f\"列表中包含{len(lst)}个元素\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 'a', 4, 'a', '4', 'c'] <class 'list'>\n",
      "列表中包含2个'a'\n",
      "列表中包含1个4\n"
     ]
    }
   ],
   "source": [
    "# 获取列表中某个元素的个数\n",
    "lst = [1, 2, 'a', 4, 'a', '4', 'c']\n",
    "\n",
    "print(lst, type(lst))\n",
    "\n",
    "print(f\"列表中包含{lst.count('a')}个'a'\")\n",
    "print(f\"列表中包含{lst.count('4')}个4\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[7, 2, 9, 1, 3] <class 'list'>\n",
      "[1, 2, 3, 7, 9] <class 'list'>\n",
      "[9, 7, 3, 2, 1] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# 列表排序\n",
    "lst = [7, 2, 9, 1, 3]\n",
    "\n",
    "print(lst, type(lst))\n",
    "\n",
    "# 正序\n",
    "lst.sort()\n",
    "print(lst, type(lst))\n",
    "\n",
    "# 倒序\n",
    "lst.sort(reverse=True)\n",
    "print(lst, type(lst))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[7, 2, 9, 1, 3] <class 'list'>\n",
      "[3, 1, 9, 2, 7] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# 列表反转\n",
    "lst = [7, 2, 9, 1, 3]\n",
    "\n",
    "print(lst, type(lst))\n",
    "\n",
    "lst.reverse()\n",
    "print(lst, type(lst))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[7, 2, 9, 1, 3] <class 'list'>\n",
      "9 is in lst => True\n",
      "11 is in lst => False\n",
      "9 is not in lst => False\n",
      "11 is not in lst => True\n"
     ]
    }
   ],
   "source": [
    "# 成员关系\n",
    "lst = [7, 2, 9, 1, 3]\n",
    "\n",
    "print(lst, type(lst))\n",
    "\n",
    "# in\n",
    "print(f\"9 is in lst => {9 in lst}\")\n",
    "print(f\"11 is in lst => {11 in lst}\")\n",
    "\n",
    "# not in\n",
    "print(f\"9 is not in lst => {9 not in lst}\")\n",
    "print(f\"11 is not in lst => {11 not in lst}\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}